<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'allotStorage:page'"
        @search="search"
        :showSearchButton="true"
      >
        <el-select
          clearable
          style="width: 200px"
          v-model="form.status"
          placeholder="库存状态"
        >
          <el-option
            v-for="item in getDict(['库存状态'])['库存状态']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <CarModel
          style="width: 350px"
          v-model="modelList"
          ref="carModel"
        />
        <el-date-picker
          v-model="form.inTime"
          type="datetimerange"
          start-placeholder="创建日期"
          end-placeholder="创建日期"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-input
          v-model="form.client"
          clearable
          placeholder="请输入调拨号"
          style="width: 200px"
        />
        <el-input
          v-model="form.vin"
          clearable
          placeholder="请输vin码"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
        @row-dblclick="handleDetails"
      >
        <template #action="{ row }">
           <i
             v-if="row.status == '待入库'"
            class="el-icon-success"
            title="确认入库"
            style="margin-left: 15px; cursor: pointer"
            @click="applyTransfer(row)"
            v-permission="'allotStorage:inStock'"
          ></i>
          <i
            v-if="row.status == '待入库'"
            class="el-icon-edit"
            title="修改"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'allotStorage:update'"
            @click="updateUser(row)"
          ></i>
        </template>
      </data-table>
      <detailsDialog :dialog-visible="dialogVisible" @set-visible="setVisible" :ruleFormDialog="ruleFormDialog"/>
      <ApplyDialog @apply-dialog="apply" :is-radio="true"/>
    </template>
  </base-layout>
</template>
<script>
import { auditIsOk } from '@/api/common'
import baseMixin from '@/mixins/base'
import { CarModel } from '@/components/index'
import detailsDialog from './components/detailsDialog'
import ApplyDialog from '@/components/ApplyDialog'

import {
  dataList, details, inStockOk
} from '@/api/pre-sale/newCarStock/putInStorageManagement/transferInventory'
import { getDict } from '@/utils/index'

export default {
  components: {
    CarModel,
    detailsDialog,
    ApplyDialog
  },
  mixins: [
    baseMixin({
      idName: 'id',
      findData: dataList,
      findDataById: details,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.brandSeriesModel = data.carBrandName + '/' + data.seriesName + '/' + data.modelName
          return data
        })
      }
    })
  ],
  data() {
    return {
      ruleFormDialog: {},
      dialogVisible: false,
      tableDataTwo: [],
      modelList: [],
      searchOptions:{ allotType: false },
      getDict: getDict,
      form: {
        vin:'',
        brandId: '',
        seriesId: '',
        modelId: ''
      },
      tableColumnAttributes: [
        {
          name: '门店',
          value: 'storeName',
          type: 'text'
        },
        {
          name: '调拨单号',
          value: 'allotCode',
          type: 'text'
        },
        {
          name: '入库单号',
          value: 'code',
          type: 'text'
        },
        {
          name: '仓库',
          value: 'storageName',
          type: 'text'
        },

        {
          name: '仓位',
          value: 'storageSiteName',
          type: 'text'
        },
        {
          name: '车架号',
          value: 'vin',
          type: 'text'
        },
        {
          name: '品牌/车系/车型',
          value: 'brandSeriesModel',
          type: 'text'
        },

        {
          name: '调拨金额',
          value: 'amount',
          type: 'text'
        },
        {
          name: '状态',
          value: 'status',
          type: 'text'
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'text'
        },
         {
          name: '创建人',
          value: 'createUserName',
          type: 'text'
        },
      ],
      dict: {}
    }
  },
  methods: {
    // 确认入库
    applyTransfer(row) {
      if (!row.storageSiteName) {
        this.$message.error('该数据的仓位为空，不能进行确认!')
        return
      }
      this.$confirm('是否确认调拨入库?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        inStockOk(row.id).then(response => {
          if (response.success) {
            this.$message.success('操作成功')
            this.setTableData()
          }
        })
      })
    },
    // 审核
    async apply(params) {
      const { success } = await auditIsOk({ ...params, sourceCode: this.currentRow.code })
      if (success) {
        this.currentRow = {}
        this.setTableData()
        this.$store.dispatch('others/setDialog', {
          visible: false,
          title: '调拨入库审核'
        })
      }
    },
    setVisible(val) {
      this.dialogVisible = val
    },
    async handleDetails(row) {
      this.dialogVisible = true
      const { data } = await details(row.id)
      const { allotCode } = row
      this.ruleFormDialog = {
        ...data, allotCode
      }
    },
    // 修改
    updateUser(row) {
      const { id } = row
      this.$router.push({
        name: 'transferInventoryInfo',
        params: { id }
      })
    },
    // 查询
    search(form) {
      if (this.modelList.length > 0) {
        form.brandId = this.modelList[0]
        form.seriesId = this.modelList[1]
        form.modelId = this.modelList[3]
      } else {
        form.brandId = ''
        form.seriesId = ''
        form.modelId = ''
      }
      if (form.inTime && form.inTime.length > 0) {
        form.inBoundMinTime = form.inTime[0]
        form.inBoundMaxTime = form.inTime[1]
      } else {
        form.inBoundMinTime = ''
        form.inBoundMaxTime = ''
      }
      delete form.inTime
      this.searchOptions = {...form,allotType: false }
      this.currentPage = 1
      this.setTableData()
    }
  }
}
</script>
